package com.imcoke.abchat.common.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author ccoke
 * @Description 文件上传下载工具类
 * @Date: 16:23 2017-11-01
 */
public class FileUtil {
    public static String SEPARATOR = "/";
    /**
     * @Author ccoke
     * @Description 将图片存储到硬盘，并返回图片url
     */
    public static String imageUpLoad(MultipartFile file,String realPath,String urlPath) throws IOException {
        if (file != null){
            //根据日期创建目录
            String dirName = createDirByDate(realPath);
            String fileName = file.getOriginalFilename();
            String fileType = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null; //截取文件后缀名
            if (fileType != null){
                if ("GIF".equals(fileType.toUpperCase()) || "PNG".equals(fileType.toUpperCase()) || "JPEG".equals(fileType.toUpperCase()) || "JPG".equals(fileType.toUpperCase()) || "BMP".equals(fileType.toUpperCase())){
                    fileName = String.valueOf(System.currentTimeMillis()) + ".png";//将图片统一保存为png
                    String filePath = realPath + SEPARATOR + dirName + SEPARATOR +  fileName;
                    String url = urlPath + dirName + SEPARATOR + fileName;
                    file.transferTo(new File(filePath)); //写入图片到磁盘
                    return url;
                }
            }
        }
        return "";
    }

    /**
     * @Author ccoke
     * @Description 将头像图片写入到保存到本地
     */
    public static String imageSave(String name,String realPath,String urlPath) throws IOException {
        String firstChar = CommonUtil.getCharByString(name);
        //根据日期创建目录
        String dirName = createDirByDate(realPath);
        String fileName =  String.valueOf(System.currentTimeMillis()) + ".png";//将图片统一保存为png
        String filePath = realPath + SEPARATOR + dirName + SEPARATOR +  fileName;
        String url = urlPath + dirName + SEPARATOR + fileName;
        try {
            BufferedImage image = FontImageUtil.createImage(firstChar);
            ImageIO.write(image,"PNG",new File(filePath));
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * @Author ccoke
     * @Description 根据日期创建目录，返回目录名称
     */
    public static String createDirByDate(String path){
        //获取日期用来创建文件夹
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String te = dateFormat.format(date);
        String dir = path + SEPARATOR +te;

        // 判断目录是否存在
        File f2 = new File(dir);
        if (!f2.exists() || !f2.isDirectory()){
            //不存在则创建目录
            f2.mkdir();
        }
        return te;
    }

    /**
     * @Author ccoke
     * @Description 删除现有图片
     */
    public static void imageDelete(String filePath){
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
        }
    }
}
